home *** CD-ROM | disk | FTP | other *** search
- Path: pangea.Stanford.EDU!karish
- From: karish@pangea.Stanford.EDU (Chuck Karish)
- Newsgroups: comp.lang.c
- Subject: Re: Looking for date & distance routines
- Date: 13 Apr 1996 09:29:07 GMT
- Organization: Mindcraft, Inc.
- Message-ID: <4kns53$ge2@nntp.Stanford.EDU>
- References: <4kn6e7$ba9@ux.accesscom.net>
- NNTP-Posting-Host: pangea.stanford.edu
-
- In article <4kn6e7$ba9@ux.accesscom.net>, Wisdom <wisdom@wisdom.com> wrote:
- >
- >I'm in search of two sets of routines:
- >
- > 1. Code to calculate the amount of days between two time_t-type
- >date variables, and a routine to return a date x days from a specified
- >time_t structure.
-
- On what type of system? If your C library has mktime(), it will
- convert data in a struct tm into a time_t, at which point you can
- subtract and divide by 84600.
-
- > 2. A routine that will calculate the distance in miles, between two
- >sets of lattitude/longitude coordinates.
-
- This function gives the result in kilometers:
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
-
-
- #define DEGTORAD 57.29578
- #define EARTHRAD 6371.
-
- double
- distance(lat1, long1, lat2, long2)
- double lat1, long1, lat2, long2;
- {
- double colat1, colat2, ang1, ang2, alpha;
-
- /* algorithm from John Osler */
- alpha=fabs((long2-long1)/DEGTORAD) ;
- colat1=(90.-lat2)/DEGTORAD;
- colat2=(90.-lat1)/DEGTORAD;
-
- ang1=cos(colat2)*cos(colat1);
- ang2=sin(colat2)*sin(colat1)*cos(alpha);
- return(acos(ang1+ang2)*EARTHRAD);
-
- }
- --
-
- Chuck Karish karish@mindcraft.com
- (415) 323-9000 x117 karish@pangea.stanford.edu
-